<?php
namespace Home\Controller;
use Home\Controller\IndexController;
class SeriveController extends IndexController {

    public function add() {
        if (IS_POST) {
            //var_dump(I('post.'));die;
            $model = D('Home/Serive');
            if ($model->create(I('post.'), 1)) {
                if ($id = $model->add()) {
                    $this->success('添加成功！', U('giveTo'));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        //取出服务类型
        $dModel = M('Dictionary');
        $typeData = $dModel->field('value')->where(array(
                    'category' => array('eq', '服务类型'),
                    'var' => array('neq', '')
                ))->select();
        $this->assign('typeData', $typeData);
        //取出当前用户名和当前时间
        $name = session('username');
        $time = time(); //时间戳
        $nowtime = date('Y-m-d H:i:s', $time); //生成带格式的日期
        $this->assign('name', $name);
        $this->assign('nowtime', $nowtime);
        $this->display();
    }

    public function edit() {
        $id = I('get.id');
        if (IS_POST) {
            $model = D('Home/Serive');
            if ($model->create(I('post.'), 2)) {
                if ($model->save() !== FALSE) {
                    $this->success('修改成功！', U('lst', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $model = M('Serive');
        $data = $model->find($id);
        $this->assign('data', $data);

        $this->setPageBtn('修改服务管理', '服务管理列表', U('lst?p=' . $p));
        $this->display();
    }

    public function delete() {
        $model = D('Home/Serive');
        if ($model->delete(I('get.id', 0)) !== FALSE) {
            $this->success('删除成功！', U('giveTo', array('p' => $p)));
            exit;
        } else {
            $this->error($model->getError());
        }
    }

    public function giveTo() {
        //传入查询的状态值用于判断下拉框
        $status = I('get.status');
        if (!$status)
            $status = '新建';
        $this->assign('status', $status);

        if (IS_POST) {
            $model = D('Home/Serive');
            if ($model->validate($model->_giveTo)->create(I('post.'), 7)) {
                if ($model->save() !== FALSE) {
                    $this->success('修改成功！', U('giveTo', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }

        $model = D('Home/Serive');
        $data = $model->search($status,1);
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        //查出客户经理
        $aModel = M('Admin');
        $adata = $aModel->field('a.id,a.username')->alias('a')->join('left join crm_admin_role b on a.id=b.admin_id')->where('role_id=4')->select();
        $this->assign('adata', $adata);
        //var_dump($adata);
        //取出服务类型
        $dModel = M('Dictionary');
        $typeData = $dModel->field('value')->where(array(
                    'category' => array('eq', '服务类型'),
                    'var' => array('neq', '')
                ))->select();
        $this->assign('typeData', $typeData);
        $this->display();
    }

    public function manage() {
        //传入查询的状态值用于判断下拉框
        $status = I('get.status');
        if (!$status)
            $status = '已分配';
        $this->assign('status', $status);

        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        $model = D('Home/Serive');
        $data = $model->search($status,1);
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        //查出客户经理
        $aModel = M('Admin');
        $adata = $aModel->field('a.id,a.username')->alias('a')->join('left join crm_admin_role b on a.id=b.admin_id')->where('role_id=4')->select();
        $this->assign('adata', $adata);
        //var_dump($adata);
        //取出服务类型
        $dModel = M('Dictionary');
        $typeData = $dModel->field('value')->where(array(
                    'category' => array('eq', '服务类型'),
                    'var' => array('neq', '')
                ))->select();
        $this->assign('typeData', $typeData);
        $this->display();
    }

    public function manageing() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        if (IS_POST) {

            $model = D('Home/Serive');
            if ($model->validate($model->yanzheng1)->create(I('post.'), 7)) {
                if ($model->save() !== FALSE) {
                    $this->success('修改成功！', U('manage', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        //传入当前用户名
        $name = session('username');
        $this->assign('name', $name);
        
        $model = D('Home/Serive');
        $data = $model->find($id);
        $this->assign('data', $data);
        $this->display();
    }

    public function onBook() {
        //传入查询的状态值用于判断下拉框
        $status = I('get.status');
        if (!$status)
            $status = '已归档';
        $this->assign('status', $status);

        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        $model = D('Home/Serive');
        $data = $model->search($status,1);
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        //查出客户经理
        $aModel = M('Admin');
        $adata = $aModel->field('a.id,a.username')->alias('a')->join('left join crm_admin_role b on a.id=b.admin_id')->where('role_id=4')->select();
        $this->assign('adata', $adata);
        //var_dump($adata);
        //取出服务类型
        $dModel = M('Dictionary');
        $typeData = $dModel->field('value')->where(array(
                    'category' => array('eq', '服务类型'),
                    'var' => array('neq', '')
                ))->select();
        $this->assign('typeData', $typeData);
        $this->display();
    }

    public function onBooking() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        $model = D('Home/Serive');
        $data = $model->find($id);
        $this->assign('data', $data);
        $this->display();
    }

    public function backTo() {
        //传入查询的状态值用于判断下拉框
        $status = I('get.status');
        if (!$status)
            $status = '已处理';
        $this->assign('status', $status);

        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        $model = D('Home/Serive');
        $data = $model->search($status,1);
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        //查出客户经理
        $aModel = M('Admin');
        $adata = $aModel->field('a.id,a.username')->alias('a')->join('left join crm_admin_role b on a.id=b.admin_id')->where('role_id=4')->select();
        $this->assign('adata', $adata);
        //var_dump($adata);
        //取出服务类型
        $dModel = M('Dictionary');
        $typeData = $dModel->field('value')->where(array(
                    'category' => array('eq', '服务类型'),
                    'var' => array('neq', '')
                ))->select();
        $this->assign('typeData', $typeData);
        $this->display();
    }

    public function backing() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
       
        $id = I('get.id');
        if (IS_POST) {
            $model = D('Home/Serive');
            $data=I('post.');
            if(I('post.manage_satisfied')>2 ){
                    $data['status']='已归档';
                };
             if(I('post.manage_satisfied')<3 ){
                    $data['status']='已分配';
                };
            if ($model->validate($model->yanzheng2)->create($data, 7)) {
                if ($model->save() !== FALSE) {
                    $this->success('操作成功！', U('backTo', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        //取出满意度
        $dModel = M('Dictionary');
        $mData = $dModel->field('value,bar')->where(array(
                    'category' => array('eq', '客户满意度'),
                    'bar' => array('neq', '')
                ))->select();
        $this->assign('mData', $mData);
        $model = D('Home/Serive');
        $data = $model->find($id);
        $this->assign('data', $data);
        $this->display();
    }

}
